import Vue from "vue"
import Vuex from "vuex"
import Cookie from 'vue-cookies'

import axios from "axios"
// Vue.prototype.$axios=axios
Vue.use(Vuex)

export default new  Vuex.Store({
  //组件通过 this.$store.state.user 调用
  state:{
    nickname:Cookie.get("nickname"),
    token:Cookie.get("token"),
    user_id:Cookie.get("user_id"),
    //Vtest组件数据
    resourceData:[],
    dateList: [],
    filterTags:[],
    tagList:null,
    ip:"http://share.youcd.me:8000/",
    // ip:"http://127.0.0.1:8000/",
  },
  mutations:{
    // 登入设置cookie
    // 组件中通过 this.$store.commit(saveToken,参数) 调用
    saveToken:function (state, userToken) {
      state.nickname=userToken.nickname;
      state.token=userToken.token;
      state.user=userToken.user;
      Cookie.set("nickname",userToken.nickname,"30min")
      Cookie.set("token",userToken.token,"30min")
      Cookie.set("user_id",userToken.user_id,"30min")
    },
    //注销 清除cookie
    clearToken:function (state) {
      state.nickname=null;
      state.token=null;
      Cookie.remove("nickname")
      Cookie.remove("token")
      Cookie.remove("user_id")
    },


    //Vtest组件的获取所有的资源数据
    getResourceData: function (state) {
        axios.request({
          url: state.ip+"resource/",
          methods: "GET"
        }).then((ret) => {
          // console.log(ret.data)
          state.resourceData = ret.data

          var date_tmp = []
          var tag_tmp = []
          for (var i = 0; i < ret.data.length; i++) {
            // console.log(ret.data[i].date)
            date_tmp.push(ret.data[i].date)
            tag_tmp.push(ret.data[i].tag)
            // console.log(ret.data[i].tag)
            // console.log(ret.data[i].date)
          }

          ///去重函数
          function unique(array) {
            var res = array.filter(function (item, index, array) {
              return array.indexOf(item) === index;
            })
            return res;
          }

          //生成date列表并格式化数据
          var date_list = unique(date_tmp)
          for (var i = 0; i < date_list.length; i++) {
            state.dateList.push({text: date_list[i], value: date_list[i]})
          }

          //生成tag列表并格式化数据
          var tag_list = unique(tag_tmp)
          for (var i = 0; i < tag_list.length; i++) {
            state.filterTags.push({text: tag_list[i], value: tag_list[i]})
            // console.log(i)
          }
          // console.log(this.datalist)
          // console.log(this.filterTags)
        }).catch((ret) => {
          alert("拉取失败")
        })

      },
      delResource:function (state,resource_id) {
        // console.log(index)
        axios.request({
          url:state.ip+"resource/"+resource_id,
          method:"DELETE",
          params:{
            token:state.token
          }
        }).then((ret)=>{
          for (var i=0;i<state.resourceData.length;i++){
            if(state.resourceData[i].id===resource_id){
              // console.log(state.resourceData[i].title,state.resourceData[i].id,resource_id)
              state.resourceData.splice(i,1)
            }
          }
          // console.log(ret.data)
          // console.log(state.resourceData.length)
          // console.log(state.resourceData)
        })
      },
      getTagList:function (state) {
        axios.request({
          url:state.ip+"tag/",
          method:"GET",
        }).then((data)=>{
          // console.log(data.data)
          state.tagList=data.data
        })
      }
      // addResource: function (state) {
      //   axios.request({
      //     url:"http://127.0.0.1:8000/resource/",
      //     method:"Post",
      //     data:{
      //       title:this.title,
      //       tag:this.tag,
      //       url:this.url,
      //       remark:this.remark,
      //       user:Number(this.$store.state.user_id)
      //     }
      //   }).then((data)=>{
      //     alert(data.data.msg)
      //   }).catch((error)=>{
      //     alert("error")
      //   })
      // }

  },

})

